package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.PatientReserveCodeDTO;
import com.pshcp.domain.model.PatientReserveCode;
import com.pshcp.mapper.PatientReserveCodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 患者预约编码表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-22
 */
@Service
public class PatientReserveCodeService {

    @Autowired
    private PatientReserveCodeMapper patientReserveCodeMapper;

    public List<PatientReserveCode> getPatientReserveCodeList() {
        Example example = new Example(PatientReserveCode.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause("type asc");
        List<PatientReserveCode> list = patientReserveCodeMapper.selectByExample(example);
        return list;
    }

    public PatientReserveCode getPatientReserveCodeByType(Integer type) {
        Example example = new Example(PatientReserveCode.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", type);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        List<PatientReserveCode> list = patientReserveCodeMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public PageList<PatientReserveCode> list(PatientReserveCodeDTO patientReserveCodeDTO) {
        if (patientReserveCodeDTO.getPageNum() != null && patientReserveCodeDTO.getPageSize() != null) {
            PageHelper.startPage(patientReserveCodeDTO.getPageNum(), patientReserveCodeDTO.getPageSize());
        }
        Example example = new Example(PatientReserveCode.class);
        Example.Criteria criteria = example.createCriteria();
        if (patientReserveCodeDTO.getStatus() != null) {
            criteria.andEqualTo("status", patientReserveCodeDTO.getStatus());
        } else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        example.setOrderByClause(" created_time desc");
        List<PatientReserveCode> list = patientReserveCodeMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<PatientReserveCode> select(PatientReserveCode patientReserveCode) {
        return patientReserveCodeMapper.select(patientReserveCode);
    }

    public int insert(PatientReserveCode patientReserveCode) {
        patientReserveCode.setId(SystemUtil.getId());
        patientReserveCode.setStatus(CommonStatusEnum.INIT.getCode());
        patientReserveCode.setCreatedTime(new Date());
        return patientReserveCodeMapper.insert(patientReserveCode);
    }

    public int update(PatientReserveCode patientReserveCode) {
        patientReserveCode.setUpdateTime(new Date());
        return patientReserveCodeMapper.updateByPrimaryKeySelective(patientReserveCode);
    }

    public PatientReserveCode findById(Long id) {
        return patientReserveCodeMapper.selectByPrimaryKey(id);
    }

    public List<PatientReserveCode> findByIds(List<Long> ids) {
        Example example = new Example(PatientReserveCode.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return patientReserveCodeMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(PatientReserveCode.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        PatientReserveCode patientReserveCode = new PatientReserveCode();
        patientReserveCode.setStatus(CommonStatusEnum.DEL.getCode());
        return patientReserveCodeMapper.updateByExampleSelective(patientReserveCode, example);
    }

}
